﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InsightAuto.Domain.Core;

namespace InsightAuto.Web.Areas.Admin.Controllers
{
    public class ArticleController : Controller
    {
        private CoreContainer db = new CoreContainer();

        public ArticleController()
        {
            ViewBag.Categories = db.ArticleCategories.ToList();
        }

        //
        // GET: /Article/

        public ViewResult Index()
        {
            return View(db.Articles.ToList());
        }

        //
        // GET: /Article/Details/5

        public ViewResult Details(int id)
        {
            Article article = db.Articles.Single(a => a.ID == id);
            return View(article);
        }

        //
        // GET: /Article/Create

        public ActionResult Create()
        {
            Article model = new Article();
            model.SubmitTime = DateTime.Now;
            return View(model);
        } 

        //
        // POST: /Article/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Article article)
        {
            int categoryId = 0;
            if (int.TryParse(this.Request.Form["Category.ID"], out categoryId))
            {
                var category = db.ArticleCategories.SingleOrDefault(ac => ac.ID == categoryId);
                article.Category = category; 
            }

            if (ModelState.IsValid)
            {
                db.Articles.AddObject(article);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(article);
        }
        
        //
        // GET: /Article/Edit/5
 
        public ActionResult Edit(int id)
        {
            Article article = db.Articles.Single(a => a.ID == id);
            return View(article);
        }

        //
        // POST: /Article/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Article article)
        {
            int categoryId = 0;
            if (int.TryParse(this.Request.Form["Category.ID"], out categoryId))
            {
                var category = db.ArticleCategories.SingleOrDefault(ac => ac.ID == categoryId);
                article.Category = category;
            }

            if (ModelState.IsValid)
            {
                db.Articles.Attach(article);
                db.ObjectStateManager.ChangeObjectState(article, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Edit", new { id = article.ID });
            }
            return View(article);
        }

        //
        // GET: /Article/Delete/5
 
        public ActionResult Delete(int id)
        {
            Article article = db.Articles.Single(a => a.ID == id);
            return View(article);
        }

        //
        // POST: /Article/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Article article = db.Articles.Single(a => a.ID == id);
            db.Articles.DeleteObject(article);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}